package com.zb.network.controller;


import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.SelectVo;
import com.zb.network.service.ConNetworkAuthenFieldService;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.utils.TokenUtil;
import com.zb.vo.NetworkAuthenFieldVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 * 网点个人认证字段表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2024-01-31
 */
@Api(tags = "服务商认证字段类接口")
@RestController
@RequestMapping("/network/authen/field")
public class ZjNetworkAuthenFieldController {
    @Resource
    private ConNetworkAuthenFieldService conNetworkAuthenFieldService;

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "服务区域-服务商设置-服务商认证字段 - 新增/修改认证字段")
    @ApiOperation("保存或更新认证字段")
    @PostMapping("save/{type}")
    public Result save(HttpServletRequest request, @PathVariable @ApiParam(value = "认证类型：1-服务商团队，2-个人服务商，3-工程师", required = true, example = "1") Integer type,
                       @RequestBody @Valid NetworkAuthenFieldVo authenFieldVo) {
        Assert.isTrue(Pattern.matches("^(1|2|3)$", type.toString()), "认证类型参数不合法");
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        conNetworkAuthenFieldService.save(custAccId, type, authenFieldVo);
        return Result.success(null == authenFieldVo.getId()? "新增认证字段成功" : "更新认证字段成功");
    }

    @ApiOperation("获取认证字段列表")
    @PostMapping("/getList/{type}")
    public Result getList(HttpServletRequest request,  @PathVariable @ApiParam(value = "认证类型：1-服务商团队，2-个人服务商，3-工程师", required = true, example = "1") Integer type,
                          @RequestBody SelectVo selectVo) {
        Assert.isTrue(Pattern.matches("^(1|2|3)$", type.toString()), "认证类型参数不合法");
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取认证字段列表成功").data(conNetworkAuthenFieldService.getList(custAccId, type, selectVo));
    }

    @ApiOperation("根据认证字段ID获取认证字段详情")
    @GetMapping("/getDetail/{type}/{id}")
    public Result getDetail(@PathVariable @ApiParam(value = "认证类型：1-服务商团队，2-个人服务商，3-工程师", required = true, example = "1") Integer type,
                            @PathVariable @ApiParam(value = "认证字段ID", required = true) Long id) {
        Assert.isTrue(Pattern.matches("^(1|2|3)$", type.toString()), "认证类型参数不合法");
        return Result.success("获取认证字段详情成功").data(PageUtil.row(conNetworkAuthenFieldService.getDetail(type, id)));
    }

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, type = OperationLogType.UPDATE, remark = "服务区域-服务商设置-服务商认证字段 - 批量启用或禁用认证字段")
    @ApiOperation("批量启用或禁用认证字段")
    @PutMapping("/disable/{type}")
    public Result disable(@PathVariable @ApiParam(value = "认证类型：1-服务商团队，2-个人服务商，3-工程师", required = true, example = "1") Integer type,
                          @RequestParam @ApiParam(value = "认证字段状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                          @RequestBody @ApiParam(value = "认证字段ID", required = true) List<Long> ids) {
        Assert.isTrue(Pattern.matches("^(1|2|3)$", type.toString()), "认证类型参数不合法");
        conNetworkAuthenFieldService.updateDisabledBatchById(type, ids, status);
        return Result.success(status? "禁用认证字段成功" : "启用认证字段成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, type = OperationLogType.UPDATE, remark = "服务区域-服务商设置-服务商认证字段 - 批量删除认证字段")
    @ApiOperation("批量删除认证字段")
    @DeleteMapping("/delete/{type}")
    public Result delete(@PathVariable @ApiParam(value = "认证类型：1-服务商团队，2-个人服务商，3-工程师", required = true, example = "1") Integer type,
                         @RequestBody @ApiParam(value = "认证字段ID", required = true) List<Long> ids) {
        Assert.isTrue(Pattern.matches("^(1|2|3)$", type.toString()), "认证类型参数不合法");
        conNetworkAuthenFieldService.deleteBatchById(type, ids);
        return Result.success("删除认证字段成功");
    }

}
